package com.clas.liu.myapplication.model.impl;

import android.os.Handler;

import com.clas.liu.myapplication.model.entity.BaseRoot;
import com.clas.liu.myapplication.model.entity.searchUser.SearchUserRoot;
import com.clas.liu.myapplication.model.event.AdministratorEvent;
import com.clas.liu.myapplication.model.i.IBandedUserModel;
import com.clas.liu.myapplication.network.NetworkInterfaces;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by liu on 2019/5/27.
 */

public class BandedUserModel extends BaseModel implements IBandedUserModel {
    public BandedUserModel(Handler handler) {
        super(handler);
    }

    @Override
    public void getInfo(int page_no, int page_size) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                AdministratorEvent event = new AdministratorEvent();
                event.setWhat(AdministratorEvent.GET_BANDED_USER_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Gson gson = new Gson();
                    SearchUserRoot root = gson.fromJson(response.body().string(), SearchUserRoot.class);  //json解析
                    AdministratorEvent event = new AdministratorEvent();                        //事件
                    event.setWhat(AdministratorEvent.GET_BANDED_USER_OK);                         //事件的设置
                    event.setSearchUserRoot(root);
                    postEvent(event);
                } else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getBandedUser(page_no, page_size, callback);
    }

    @Override
    public void cancel(int id) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                AdministratorEvent event = new AdministratorEvent();
                event.setWhat(AdministratorEvent.CANCEL_BANDED_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Gson gson = new Gson();
                    BaseRoot root = gson.fromJson(response.body().string(), BaseRoot.class);  //json解析
                    AdministratorEvent event = new AdministratorEvent();                        //事件
                    event.setWhat(AdministratorEvent.CANCEL_BANDED_OK);                         //事件的设置
                    event.setBaseRoot(root);
                    postEvent(event);
                } else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.cancelBanded(id, callback);
    }

    @Override
    protected NetworkInterfaces getNetworkInterface() {
        return new NetworkInterfaces();
    }
}
